<?php

namespace GroupBuying;

use C\L\AdmController;
use C\M\GroupBuyingGoods;

class GoodsController extends AdmController
{
    /**
     * 列表、搜索接口
     **/
    public function indexAction()
    {
        $currentPage = $this->request->getQuery('page', 'int', 1);
        $perPage     = $this->request->getQuery('per_page', 'int', 20);
        $keyword     = $this->request->getQuery('keyword', 'string', null);
        $status      = $this->request->getQuery('status', 'string', null);

        $columns = [
            'id', 'thumbnail', 'title', 'description', 'price', 'status', 'sort', 'stock',
        ];

        $builder = $this
          ->modelsManager
          ->createBuilder()
          ->from('C\M\GroupBuyingGoods')
          ->columns(implode(',', $columns))
          ->orderBy('sort ASC, id DESC');

        if ($keyword) {
            $builder = $builder->andWhere('title like \'%' . $keyword . '%\'');
        }
        if ($status) {
            $builder = $builder->andWhere('status = \'' . $status . '\'');
        }

        $result = $this->paginationByBuilder($builder, $currentPage, $perPage);

        return $this->success($result);
    }

    /**
     * 详情接口
     **/
    public function showAction()
    {
        $id = $this->request->getQuery('id');

        $goods = GroupBuyingGoods::findFirst($id);
        $goods->other_info = json_decode($goods->other_info);

        $goods
            ? $this->success($goods)
            : $this->error();
    }

    /**
     * 创建接口
     **/
    public function createAction()
    {
        $request = $this->request;

        $otherInfo = [
            'slave_goods_id'              => $request->getPost('slave_goods_id', 'int', 0),
            'contract_id'                 => $request->getPost('contract_id', 'int', 0),
            'commission_rate'             => $request->getPost('commission_rate', 'float', 0),
            'commission_period'           => $request->getPost('commission_period', 'int', 0),
            'commission_type'             => $request->getPost('commission_type', 'string', null),
            'commission_vip_bonus'        => $request->getPost('commission_vip_bonus', 'string', null),
            'group_buying_period'         => $request->getPost('group_buying_period', 'int', 0),
            'group_buying_quantity_min'   => $request->getPost('group_buying_quantity_min', 'int', 0),
            'group_buying_quantity_max'   => $request->getPost('group_buying_quantity_max', 'int', 0),
            'group_buying_limit_type'     => $request->getPost('group_buying_limit_type', 'string', 'by_user'),
            'group_buying_limit_quantity' => $request->getPost('group_buying_limit_quantity', 'int', 0),
            'group_buying_limit_point'    => $request->getPost('group_buying_limit_point', 'int', 0),
            'reward_user'                 => $request->getPost('reward_user', 'float', 0),
            'reward_superior'             => $request->getPost('reward_superior', 'float', 0),
        ];

        $goods = new GroupBuyingGoods();

        $goods->thumbnail   = $request->getPost('thumbnail', 'string', null);
        $goods->title       = $request->getPost('title', 'string', null);
        $goods->description = $request->getPost('description', 'string', null);
        $goods->price       = $request->getPost('price', 'float', 0.00);
        $goods->stock       = $request->getPost('stock', 'int', 0);
        $goods->sales       = $request->getPost('sales', 'int', 0);
        $goods->content     = $request->getPost('content', 'string', null);
        $goods->sort        = $request->getPost('sort', 'int', 99);
        $goods->status      = 'off_sale';
        $goods->other_info  = json_encode($otherInfo);

        $goods->save()
            ? $this->success()
            : $this->error();
    }

    /**
     * 更新接口
     **/
    public function updateAction()
    {
        $request = $this->request;

        $id = $request->getPost('id');

        $goods = GroupBuyingGoods::findFirst($id);
        if (!$goods) {
            return $this->error('未找到商品');
        }

        $otherInfo = [
            'slave_goods_id'              => $request->getPost('slave_goods_id', 'int', 0),
            'contract_id'                 => $request->getPost('contract_id', 'int', 0),
            'commission_rate'             => $request->getPost('commission_rate', 'float', 0),
            'commission_period'           => $request->getPost('commission_period', 'int', 0),
            'commission_type'             => $request->getPost('commission_type', 'string', null),
            'commission_vip_bonus'        => $request->getPost('commission_vip_bonus', 'string', null),
            'group_buying_period'         => $request->getPost('group_buying_period', 'int', 0),
            'group_buying_quantity_min'   => $request->getPost('group_buying_quantity_min', 'int', 0),
            'group_buying_quantity_max'   => $request->getPost('group_buying_quantity_max', 'int', 0),
            'group_buying_limit_type'     => $request->getPost('group_buying_limit_type', 'string', 'by_user'),
            'group_buying_limit_quantity' => $request->getPost('group_buying_limit_quantity', 'int', 0),
            'group_buying_limit_point'    => $request->getPost('group_buying_limit_point', 'int', 0),
            'reward_user'                 => $request->getPost('reward_user', 'float', 0),
            'reward_superior'             => $request->getPost('reward_superior', 'float', 0),
        ];

        $goods->thumbnail   = $request->getPost('thumbnail', 'string', null);
        $goods->title       = $request->getPost('title', 'string', null);
        $goods->description = $request->getPost('description', 'string', null);
        $goods->price       = $request->getPost('price', 'float', 0.00);
        $goods->stock       = $request->getPost('stock', 'int', 0);
        $goods->sales       = $request->getPost('sales', 'int', 0);
        $goods->content     = $request->getPost('content', 'string', null);
        $goods->sort        = $request->getPost('sort', 'int', 99);
        $goods->other_info  = json_encode($otherInfo);

        $goods->save()
            ? $this->success()
            : $this->error();
    }

    /**
     * 删除接口
     **/
    public function deleteAction()
    {
        $id = $this->request->getPost('id');

        $phql = 'DELETE FROM C\M\GroupBuyingGoods WHERE id = :id:';

        $result = $this->modelsManager->executeQuery($phql, compact('id'));

        return $result->success() ? $this->success() : $this->error();
    }

    /**
     * 上架接口
     **/
    public function onSaleAction()
    {
        $id = $this->request->getPost('id');

        $phql = 'UPDATE C\M\GroupBuyingGoods SET status = "on_sale" WHERE id = :id:';

        $result = $this->modelsManager->executeQuery($phql, compact('id'));

        return $result->success() ? $this->success() : $this->error();
    }

    /**
     * 下架接口
     **/
    public function offSaleAction()
    {
        $id = $this->request->getPost('id');

        $phql = 'UPDATE C\M\GroupBuyingGoods SET status = "off_sale" WHERE id = :id:';

        $result = $this->modelsManager->executeQuery($phql, compact('id'));

        return $result->success() ? $this->success() : $this->error();
    }

    /**
     * 预售接口
     **/
    public function preSaleAction()
    {
        $id = $this->request->getPost('id');

        $phql = 'UPDATE C\M\GroupBuyingGoods SET status = "pre_sale" WHERE id = :id:';

        $result = $this->modelsManager->executeQuery($phql, compact('id'));

        return $result->success() ? $this->success() : $this->error();
    }

    /**
     * 批量上架接口
     **/
    public function bulkOnSaleAction()
    {
        $ids = $this->request->getPost('ids', null, []);

        $phql = 'UPDATE C\M\GroupBuyingGoods SET status = "on_sale" WHERE id IN (:ids:)';
        $result = $this->modelsManager->executeQuery($phql, [
            'ids' => implode(', ', $ids),
        ]);

        return $result->success() ? $this->success() : $this->error();
    }

    /**
     * 批量下架接口
     **/
    public function bulkOffSaleAction()
    {
        $ids = $this->request->getPost('ids', null, []);

        $phql = 'UPDATE C\M\GroupBuyingGoods SET status = "off_sale" WHERE id IN (:ids:)';
        $result = $this->modelsManager->executeQuery($phql, [
            'ids' => implode(', ', $ids),
        ]);

        return $result->success() ? $this->success() : $this->error();
    }
}
